package com.shengdou.as.slopsapptest.Fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okhttputils.OkHttpUtils;
import com.shengdou.as.slopsapptest.Activity.LoginActivity;
import com.shengdou.as.slopsapptest.MyApplication;
import com.shengdou.as.slopsapptest.R;
import com.shengdou.as.slopsapptest.base.BaseActivity;
import com.shengdou.as.slopsapptest.base.BaseFragment;
import com.shengdou.as.slopsapptest.bean.FactoryNameBean;
import com.shengdou.as.slopsapptest.bean.PoolDataBean;
import com.shengdou.as.slopsapptest.bean.PoolNameBean;
import com.shengdou.as.slopsapptest.bean.PoolPushBean;
import com.shengdou.as.slopsapptest.bean.person;
import com.shengdou.as.slopsapptest.utils.ArrayListSplit;
import com.shengdou.as.slopsapptest.utils.Constants;
import com.shengdou.as.slopsapptest.utils.FileUtils;
import com.shengdou.as.slopsapptest.utils.HttpUtils;
import com.shengdou.as.slopsapptest.utils.LogUtil;
import com.shengdou.as.slopsapptest.utils.NetConstant;
import com.shengdou.as.slopsapptest.utils.SharedPreferenceUtil;
import com.shengdou.as.slopsapptest.utils.ToastUtils;
import com.shengdou.as.slopsapptest.view.MyImageView;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;

/**
 * 水池列表
 */
public class CheckFragment extends BaseFragment {
    private ListView mGrid;
    private TextView mFactory;
    private PopupWindow popup = null;
    private LinearLayout gone;
    private ArrayList<PoolDataBean.DataBean> arrayList = new ArrayList<>();
    private ArrayList<ArrayList<PoolDataBean.DataBean>> listHeight = new ArrayList<>();
    private ArrayList<FactoryNameBean.AddrBean> FactoryArray;
    private PoolNameBean managerInfo;
    private PoolDataBean.DataBean poolDataBean;
    private MyAdapters adapters;
    private mFactoryNameAdapter factoryNameAdapter;
    private boolean selector;
    private Map<String, String> params = new HashMap<>();
    private PoolTabFragment poolTabFragment = new PoolTabFragment();
    private Message message;
    private String factoryUrl, factoryName, mobile;
    int playMusicState = 0;//为只获取一次保存的数据
    private boolean playSelect;//获取报警状态是否一响过
    private MediaPlayer music = null;// 播放器引用
    int aNum = 0;
    int poolsid;//设置泥位高度id默认为-1
    private String state = null;
    public Handler mHandler;

    {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case Constants.POOLLIST:
                        //获取名字的
                        String result = msg.obj.toString();
                        LogUtil.e("获取名字的", result);
                        managerInfo = new Gson().fromJson(result, PoolNameBean.class);
                        LogUtil.e("系统时间", systems());
                        arrayList.clear();
                        listHeight.clear();
                        poolDataBean = null;
                        if (managerInfo.getStatus() == 1) {
                            String alias = managerInfo.getAlias();
                            SharedPreferenceUtil.savePreferenceString(getActivity(), "alias", alias);//域名jpush
                            //设置别名Alia
                            JPushInterface.setAlias(getActivity(), 0, alias);
                        }
                        if (managerInfo.getData() != null && managerInfo.getData().size() > 0) {
                            state = "请求数据成功";
                            for (int i = 0; i < managerInfo.getData().size(); i++) {
                                poolDataBean = new PoolDataBean.DataBean();
                                poolDataBean.setId(managerInfo.getData().get(i).getId());
                                poolDataBean.setName(managerInfo.getData().get(i).getName());
                                arrayList.add(poolDataBean);
                            }
                            if (arrayList != null && arrayList.size() > 0) {
                                boolean flag = FileUtils.saveObject(getActivity(), "mArray", arrayList);
                                LogUtil.e("flag", flag + "arrayList" + arrayList.get(0).getId() + "--" + arrayList.size());
                            }

                            message = Message.obtain();
                            message.what = 4;
                            message.obj = arrayList;
                            poolTabFragment.mHandler.sendMessage(message);
                            listHeight.addAll(ArrayListSplit.split(arrayList, 2));
                            LogUtil.e("list.size", listHeight.size() + "");
                            if (adapters == null) {
                                adapters = new MyAdapters(listHeight);
                                mGrid.setAdapter(adapters);
                                LogUtil.e("list.size", listHeight.size() + "");
                            }
                            if (arrayList.size() != 0) {
                                adapters.notifyDataSetChanged();
                                mGrid.setVisibility(View.VISIBLE);
                                gone.setVisibility(View.GONE);
                            } else {
                                adapters.notifyDataSetChanged();
                                mGrid.setVisibility(View.GONE);
                                gone.setVisibility(View.VISIBLE);
                            }
                            aNum = 0;
                            HttpUtils.posts(mHandler, NetConstant.POOLPUSH(), Constants.POOLPUSH, getActivity());
                        } else {
                            mGrid.setVisibility(View.GONE);
                            gone.setVisibility(View.VISIBLE);
                        }
                        break;
                    case Constants.SETFACTORY:
                        //设置厂名
                        String results = msg.obj.toString();
                        LogUtil.e("设置厂名results", results);
                        params.clear();
                        try {
                            JSONObject jsonObject = new JSONObject(results);
                            ToastUtils.showToast(jsonObject.getString("status_text"));
                            SharedPreferenceUtil.savePreferenceString(getActivity(), "FactoryName", factoryName);
                            mFactory.setText(factoryName);
                            adapters = null;
                            listHeight.clear();
                            selector = true;
                            aNum = 0;
                            HttpUtils.post(mHandler, NetConstant.POOLLIST(), Constants.POOLLIST, getActivity());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        break;
                    case Constants.FACTORYADD:
                        //获取所有厂的信息
                        LogUtil.e("===厂名信息=====", msg.obj.toString());
                        FactoryNameBean factoryNameBean = new Gson().fromJson(msg.obj.toString(), FactoryNameBean.class);
                        FactoryArray = new ArrayList<>();
                        if (factoryNameBean.getAddr() != null) {
                            FactoryArray.addAll(factoryNameBean.getAddr());
                            factoryNameAdapter.setmArray(FactoryArray);

                        }
                        break;
                    case Constants.POOLPUSH:
                        //获取泥位高度
                        ++aNum;
                        String response = msg.obj.toString();
                        LogUtil.e("获取泥位高度", aNum + response);
                        arrayList.clear();
                        listHeight.clear();
                        poolDataBean = null;
                        PoolPushBean pool = new Gson().fromJson(response, PoolPushBean.class);
                        if ((managerInfo.getData() != null && managerInfo.getData().size() > 0) &&
                                (pool.getData() != null && pool.getData().size() > 0)) {
                            for (int i = 0; i < managerInfo.getData().size(); i++) {
                                poolDataBean = new PoolDataBean.DataBean();
                                for (int j = 0; j < pool.getData().size(); j++) {
                                    if (managerInfo.getData().get(i).getId() == pool.getData().get(j).getPools_id()) {
                                        poolDataBean.setId(managerInfo.getData().get(i).getId());
                                        poolDataBean.setName(managerInfo.getData().get(i).getName());
                                        poolDataBean.setPools_id(pool.getData().get(j).getPools_id());
                                        poolDataBean.setAlarm(pool.getData().get(j).getAlarm());
                                        poolDataBean.setStatus(pool.getData().get(j).getStatus());
                                        poolDataBean.setHeight(pool.getData().get(j).getHeight());
                                    }
                                }
                                arrayList.add(poolDataBean);
                            }
                       /* if (playMusicState == 0) {
                            playSelect = SharedPreferenceUtil.getPreferenceBoolean(getActivity(), "playSelect");
                            playMusicState += 1;
                        }*/
//                        SaveHeight = SharedPreferenceUtil.getPreferenceFloat(getActivity(), "newHeight");
                            //判断报警铃声
                            for (int j = 0; j < arrayList.size(); j++) {
                                if (arrayList.get(j).getAlarm() == 2 && !music.isPlaying()) {
                                    if (playSelect == true && selector == true) {
                                        PlayMusic(R.raw.warning_high);
                                        music.start();
                                        dialogMusic();
                                    } else {
                                        music.stop();
//                                        playSelect = true;
                                    }
                                }
                            }
                            int poolsids = poolsid;//获取默认的id
                            listHeight.addAll(ArrayListSplit.split(arrayList, 2));
                            LogUtil.e(listHeight.size() + "list.size", poolsids + "---" + listHeight.get(0).get(0).getPools_id());
                            if (adapters == null) {
                                adapters = new MyAdapters(listHeight);
                                mGrid.setAdapter(adapters);
                            }
                            if (arrayList.size() != 0) {
                                mGrid.setVisibility(View.VISIBLE);
                                gone.setVisibility(View.GONE);
                                LogUtil.e("selector=====selector=======", selector + "");
                                //判断id和获取的id是否一致
                                if (poolsids != listHeight.get(0).get(0).getPools_id()
                                        && listHeight.get(0).get(0).getPools_id() != 0) {
                                    LogUtil.e(listHeight.get(0).get(0).getPools_id() + "aNum", poolsids + "aNum");
                                    poolsid = listHeight.get(0).get(0).getPools_id();//将原始的id切换为获取的id
                                    adapters.notifyDataSetChanged();
                                }
                                //判断id和获取的id一致更新适配器
                                if (poolsids == listHeight.get(0).get(0).getPools_id()
                                        && listHeight.get(0).get(0).getPools_id() != 0) {
                                    LogUtil.e(listHeight.get(0).get(0).getPools_id() + "", poolsids + "--" + aNum);
                                    adapters.notifyDataSetChanged();
                                }
                                if (selector == true) {
                                    new Thread(new Runnable() {
                                        public void run() {
                                            LogUtil.e("系统时间============", systems());
                                            HttpUtils.posts(mHandler, NetConstant.POOLPUSH(), Constants.POOLPUSH, getActivity());
                                            LogUtil.e("aaaaaaaaaaaa+++", aNum + 1 + "");

                                        }
                                    }).start();
                                }
                            } else {
                                if (poolsid != listHeight.get(0).get(0).getPools_id()) {
                                    adapters.notifyDataSetChanged();
                                }
                                mGrid.setVisibility(View.GONE);
                                gone.setVisibility(View.VISIBLE);
                            }
                        } else {
                            mGrid.setVisibility(View.GONE);
                            gone.setVisibility(View.VISIBLE);
                        }
                        break;
                }
            }
        };
    }

    private void dialogMusic() {
        final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
        alertDialog.setCancelable(false);
        alertDialog.show();
        alertDialog.getWindow().setContentView(R.layout.dialog_music);
        alertDialog.getWindow().findViewById(R.id.dialog_verify_go).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                music.stop();
                SharedPreferenceUtil.savePreferenceBoolean(getActivity(), "playSelect", false);
                playSelect = SharedPreferenceUtil.getPreferenceBoolean(getActivity(), "playSelect");
                alertDialog.cancel();

            }
        });
    }

    public String systems() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日    HH:mm:ss     ");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_check, null);
    }

    @Override
    protected void initFindViewById(View view) {
        mobile = SharedPreferenceUtil.getPreferenceString(getActivity(), "mobile");
        factoryName = SharedPreferenceUtil.getPreferenceString(getActivity(), "FactoryName");
        mFactory = (TextView) view.findViewById(R.id.factory_check);
        mFactory.setText(factoryName);
        mFactory.setOnClickListener(this);
        mGrid = (ListView) view.findViewById(R.id.listview);
        adapters = new MyAdapters(listHeight);
        gone = (LinearLayout) view.findViewById(R.id.txt_gone);
        mGrid.setAdapter(adapters);
    }

    @Override
    public void initData() {
        PlayMusic(R.raw.warning_high);
        //将泥位高度的id保存为-1
        poolsid = -1;
        LogUtil.e("initdata==========CheckFragment===================", "initdata");
        selector = true;
        playSelect = SharedPreferenceUtil.getPreferenceBoolean(getActivity(), "playSelect");
        LogUtil.e("playSelect===========", playSelect + "");
        LogUtil.e(SharedPreferenceUtil.getPreferenceString(getActivity(), "ipInfo") + "==================" + NetConstant.ip);
        String ipInfo = SharedPreferenceUtil.getPreferenceString(getActivity(), "ipInfo");
        if (ipInfo == null || ipInfo.equals("")) {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            getActivity().finish();
        } else {
            HttpUtils.postspool(mHandler, NetConstant.POOLLIST(), Constants.POOLLIST, getActivity());

        }
    }

    /**
     * 播放音乐，参数是资源id
     *
     * @param MusicId
     */
    private void PlayMusic(int MusicId) {
        music = MediaPlayer.create(getActivity(), MusicId);
//        music.reset(); //重置多媒体


    }

    //切换厂名
    private void showPopup() {
        selector = false;
        OkHttpUtils.getInstance().cancelTag(NetConstant.POOLPUSH());//取消泥位高度请求
        if (popup != null && popup.isShowing()) {
            popup.dismiss();
        } else {
            LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View popview = inflater.inflate(R.layout.pop_list, null);
            ListView mListview = (ListView) popview.findViewById(R.id.listview);
            factoryNameAdapter = new mFactoryNameAdapter(popup);
            mListview.setAdapter(factoryNameAdapter);
            HttpUtils.post(mHandler, NetConstant.FACTORYADD, params, Constants.FACTORYADD, getActivity());
            popup = new PopupWindow(popview, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            popup.setFocusable(true);
//          popup.setOutsideTouchable(true);
            popup.setBackgroundDrawable(new BitmapDrawable());
            popup.showAsDropDown(mFactory);

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.factory_check:
                showPopup();//切换厂名
//
                break;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtil.e("onPause==========CheckFragment===================", "onPause");
        selector = false;
        playSelect = false;
        OkHttpUtils.getInstance().cancelTag(NetConstant.POOLPUSH());//取消泥位高度请求
        music.stop();
        LogUtil.e("selector=====onPause=======", selector + "");
//        OkHttpUtils.getInstance().cancelTag(NetConstant.POOLPUSH);
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtil.e("onStop========CheckFragment============", "onStop");
        selector = false;
        LogUtil.e("selector=====onStop=======", selector + "");
    }

    @Override
    public void onResume() {
        super.onResume();
        poolsid = -1;//将泥位高度的id保存为-1
        if (selector == false) {
            selector = true;
            playSelect = SharedPreferenceUtil.getPreferenceBoolean(getActivity(), "playSelect");
            LogUtil.e("playSelect===========", playSelect + "");
            if (state != null) {
                LogUtil.e("state===========", state + "");
                HttpUtils.posts(mHandler, NetConstant.POOLPUSH(), Constants.POOLPUSH, getActivity());
            }
        }
        LogUtil.e("onResume=====CheckFragment=========", "onResume");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.e("onDestroy", "onDestroy");
    }

    //泥位高度适配
    private class MyAdapters extends BaseAdapter {
        private ArrayList<ArrayList<PoolDataBean.DataBean>> mArray;
        int num;

        public MyAdapters(ArrayList<ArrayList<PoolDataBean.DataBean>> mAarrayList) {
            this.mArray = mAarrayList;
            num = arrayList.size() % 2;
            LogUtil.e("==num===" + num);
        }

        @Override
        public int getCount() {
            return mArray != null ? mArray.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mArray.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemViewType(int position) {
            num = arrayList.size() % 2;
            if (num == 1) {
                if (position == (mArray.size() - 1)) {
                    LogUtil.e("========1=========" + position);
                    return 1;
                } else if (position < (mArray.size() - 1)) {
                    LogUtil.e("========0=========" + position);
                    return 0;
                }
            } else {
                LogUtil.e("========num=========" + position);
                return 0;
            }
            return 4;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final ViewHolder holder;
            LogUtil.e(getItemViewType(position) + "getItemViewType(position)");
            if (getItemViewType(position) == 0) {
                if (convertView == null) {
                    holder = new ViewHolder();
                    convertView = View.inflate(mActivity, R.layout.listview_item_gridview, null);
                    int width = BaseActivity.screenWidth;
                    int height = BaseActivity.screenHeight / 5;
                    convertView.setLayoutParams(new AbsListView.LayoutParams(width, height));
//                    LogUtil.e("shuju shenme ", width + "-----" + height);
                    holder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.relative);
                    holder.relativeLayout2 = (RelativeLayout) convertView.findViewById(R.id.relative1);
                    holder.mImage = (MyImageView) convertView.findViewById(R.id.image);
                    holder.mImages = (MyImageView) convertView.findViewById(R.id.image1);
                    holder.mTxtType = (TextView) convertView.findViewById(R.id.txt_type);
                    holder.mTxtMud = (TextView) convertView.findViewById(R.id.txt_mud);
                    holder.mTxtpool = (TextView) convertView.findViewById(R.id.txt_pool);
                    holder.mTxtpool2 = (TextView) convertView.findViewById(R.id.txt_pool1);
                    holder.mTxtType2 = (TextView) convertView.findViewById(R.id.txt_type1);
                    holder.mTxtMud2 = (TextView) convertView.findViewById(R.id.txt_mud1);

                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                float height = mArray.get(position).get(0).getHeight();
                if (mArray.get(position).get(0).getStatus() == 1) {
                    holder.mTxtType.setText("系统在线");
                } else if (mArray.get(position).get(0).getStatus() == 200) {
                    holder.mTxtType.setText("自动清洗");
                } else if (mArray.get(position).get(0).getStatus() == 201) {
                    holder.mTxtType.setText("系统离线");
                } else if (mArray.get(position).get(0).getStatus() == 202) {
                    holder.mTxtType.setText("系统故障");
                }
                if (height >= 0) {
                    holder.mTxtMud.setText("泥位" + height + "米");
                } else {
                    holder.mTxtMud.setText("泥位0.0米");
                }
                if (height > 0) {
                    holder.mImage.setWind(mArray.get(position).get(0).getHeight(), mArray.get(position).get(0).getAlarm());
                } else {
                    holder.mImage.setWind(0, -1);
                }
                holder.mTxtpool.setText(mArray.get(position).get(0).getName());
                holder.mTxtpool2.setText(mArray.get(position).get(1).getName());
                float height1 = mArray.get(position).get(1).getHeight();
                if (mArray.get(position).get(1).getStatus() == 1) {
                    holder.mTxtType2.setText("系统在线");
                } else if (mArray.get(position).get(1).getStatus() == 200) {
                    holder.mTxtType2.setText("自动清洗");
                } else if (mArray.get(position).get(1).getStatus() == 201) {
                    holder.mTxtType2.setText("系统离线");
                } else if (mArray.get(position).get(1).getStatus() == 202) {
                    holder.mTxtType2.setText("系统故障");
                }
                if (height1 >= 0) {
                    holder.mTxtMud2.setText("泥位" + height1 + "米");
                } else {
                    holder.mTxtMud.setText("泥位0.0米");
                }
                if (height1 > 0) {
                    holder.mImages.setWind(mArray.get(position).get(1).getHeight(), mArray.get(position).get(1).getAlarm());
                } else {
                    holder.mImages.setWind(0, -1);
                }
                final int index = position;
                holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Bundle bundle = new Bundle();
                        // 传递int类型数
                        bundle.putInt("pools_id", mArray.get(index).get(0).getId());
                        bundle.putString("pools_name", mArray.get(index).get(0).getName());
                        //设置数据
                        poolTabFragment.setArguments(bundle);
                        //调用上面的方法由 fragment1 跳转到 fragment2
                        showFragment(CheckFragment.this, poolTabFragment);
                    }
                });
                holder.relativeLayout2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        // 传递int类型数
                        bundle.putInt("pools_id", mArray.get(index).get(1).getId());
                        bundle.putString("pools_name", mArray.get(index).get(1).getName());
                        //设置数据
                        poolTabFragment.setArguments(bundle);
                        //调用上面的方法由 fragment1 跳转到 fragment2
                        showFragment(CheckFragment.this, poolTabFragment);
                    }
                });
            } else if (getItemViewType(position) == 1) {
                if (convertView == null) {
                    holder = new ViewHolder();
                    convertView = View.inflate(mActivity, R.layout.item_check_infos, null);
                    int width = BaseActivity.screenWidth;
                    int height = BaseActivity.screenHeight / 5;
                    convertView.setLayoutParams(new AbsListView.LayoutParams(width, height));
                    holder.mImage = (MyImageView) convertView.findViewById(R.id.image);
                    holder.mTxtpool = (TextView) convertView.findViewById(R.id.txt_pool);
                    holder.mTxtType = (TextView) convertView.findViewById(R.id.txt_type);
                    holder.mTxtMud = (TextView) convertView.findViewById(R.id.txt_mud);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                float height = mArray.get(position).get(0).getHeight();
                if (mArray.get(position).get(0).getStatus() == 1) {
                    holder.mTxtType.setText("系统在线");
                } else if (mArray.get(position).get(0).getStatus() == 200) {
                    holder.mTxtType.setText("自动清洗");
                } else if (mArray.get(position).get(0).getStatus() == 201) {
                    holder.mTxtType.setText("系统离线");
                } else if (mArray.get(position).get(0).getStatus() == 202) {
                    holder.mTxtType.setText("系统故障");
                }
                if (height >= 0) {
                    holder.mTxtMud.setText("泥位" + height + "米");
                } else {
                    holder.mTxtMud.setText("泥位0.0米");
                }
                if (height > 0) {
                    holder.mImage.setWind(mArray.get(position).get(0).getHeight(), mArray.get(position).get(0).getAlarm());
                } else {
                    holder.mImage.setWind(0, -1);
                }
                holder.mTxtpool.setText(mArray.get(position).get(0).getName());
                final int index = position;
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Bundle bundle = new Bundle();
                        // 传递int类型数
                        bundle.putInt("pools_id", mArray.get(index).get(0).getId());
                        bundle.putString("pools_name", mArray.get(index).get(0).getName());
                        //设置数据
                        poolTabFragment.setArguments(bundle);
                        //调用上面的方法由 fragment1 跳转到 fragment2
                        showFragment(CheckFragment.this, poolTabFragment);
                    }
                });
            }
            return convertView;
        }
    }

    class ViewHolder {
        RelativeLayout relativeLayout, relativeLayout2;
        MyImageView mImage, mImages;
        //        MyImageView mImages;
        TextView mTxtpool;
        TextView mTxtType;
        TextView mTxtMud;
        TextView mTxtpool2;
        TextView mTxtType2;
        TextView mTxtMud2;
        //厂名信息
        TextView mTxtName;
    }

    /**
     * 公共方法： 从碎片fragment1跳转到碎片fragment2
     *
     * @param checkFragment   当前fragment
     * @param poolTabFragment 跳转后的fragment
     */
    public void showFragment(CheckFragment checkFragment, PoolTabFragment poolTabFragment) {
        selector = false;
        mFactory.setClickable(false);
        OkHttpUtils.getInstance().cancelTag(NetConstant.POOLPUSH());//取消泥位高度请求
        // 获取 FragmentTransaction  对象
        FragmentTransaction transaction = getFragmentManager()
                .beginTransaction();
        //如果fragment2没有被添加过，就添加它替换当前的fragment1
        if (!poolTabFragment.isAdded()) {
            LogUtil.e("======poolTabFragment.isAdded()=============");
            transaction.add(R.id.fragment_container, poolTabFragment)
                    // 移除fragment1，即当前碎片
                    .remove(checkFragment)
                    //加入返回栈，这样你点击返回键的时候就会回退到fragment1了
                    .addToBackStack(null)
                    // 提交事务
                    .commitAllowingStateLoss();

        } else { //如果已经添加过了的话就隐藏fragment1，显示fragment2
            transaction
                    // 隐藏fragment1，即当前碎片
                    .hide(checkFragment)
                    // 显示已经添加过的碎片，即fragment2
                    .show(poolTabFragment)
                    // 加入返回栈
                    .addToBackStack(null)
                    // 提交事务
                    .commitAllowingStateLoss();
        }
    }

    //为切换厂名
    private class mFactoryNameAdapter extends BaseAdapter {
        private ArrayList<FactoryNameBean.AddrBean> mArray = new ArrayList<>();
        private PopupWindow popups;

        public mFactoryNameAdapter(PopupWindow popup) {
            this.popups = popup;
        }

        public void setmArray(ArrayList<FactoryNameBean.AddrBean> factoryArray) {
            this.mArray.addAll(factoryArray);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mArray.size();
        }

        @Override
        public Object getItem(int position) {
            return mArray.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(mActivity, R.layout.item_factory_name, null);
                holder.mTxtName = (TextView) convertView.findViewById(R.id.pop_name);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.mTxtName.setText(mArray.get(position).getName());
            final int index = position;
            holder.mTxtName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    LogUtil.e("factory_id", mArray.get(index).getId() + "");
//                    params.put("factory_id", mArray.get(index).getId() + "");
                    selector = false;
                    OkHttpUtils.getInstance().cancelTag(NetConstant.POOLPUSH());//取消泥位高度请求
                    factoryName = mArray.get(index).getName();
                    factoryUrl = mArray.get(index).getUrl();
                    if (factoryUrl != null && !factoryUrl.equals("")) {
                        SharedPreferenceUtil.savePreferenceString(MyApplication.ApplicationContext, "ipInfo", factoryUrl);
                    }
                    SharedPreferenceUtil.savePreferenceBoolean(getActivity(), "playSelect", true);
                    playSelect = SharedPreferenceUtil.getPreferenceBoolean(getActivity(), "playSelect");
                    LogUtil.e("playSelect===========", playSelect + "");
                    aNum = 0;
                    poolsid = -1;//将泥位高度的id保存为-1
                    params.put("mobile", mobile);
                    HttpUtils.post(mHandler, NetConstant.SETFACTORY(), params, Constants.SETFACTORY, getActivity());
                    if (popup != null && popup.isShowing()) {
                        popup.dismiss();
                    }
                }
            });
            return convertView;
        }
    }

    //获取厂名信息
    public ArrayList<person.FactoryBean> getFactory() {
        ArrayList<person.FactoryBean> FactoryArray = null;
        Object obj = FileUtils.getObject(getActivity(), "person");
        person person = null;
        if (obj instanceof person) {
            person = (person) obj;
        }
        if (person != null) {
//            FactoryArray = person.getFactory();
            return FactoryArray;
        } else {
            return null;
        }

    }
}
